#include "ints.h"


int install_asm_irq_handler(int irq, void (*irq_func)(void), _go32_dpmi_seginfo *old_irq) 
{
	_go32_dpmi_seginfo new_vec;
	
	_go32_dpmi_get_protected_mode_interrupt_vector( IRQ_VECTOR(irq) , old_irq);
	
	new_vec.pm_selector = _go32_my_cs();
	new_vec.pm_offset = (unsigned long) irq_func;
	
	_go32_dpmi_set_protected_mode_interrupt_vector ( IRQ_VECTOR(irq) , &new_vec);
	
	return 0;
}




int install_c_irq_handler(int irq, void (*irq_func)(void), _go32_dpmi_seginfo *old_irq) 
{
	_go32_dpmi_seginfo new_vec;
	
	_go32_dpmi_get_protected_mode_interrupt_vector( IRQ_VECTOR(irq) , old_irq);
	
	new_vec.pm_selector = _go32_my_cs();
	new_vec.pm_offset = (unsigned long) irq_func;
	
	_go32_dpmi_allocate_iret_wrapper(&new_vec);
	
	_go32_dpmi_set_protected_mode_interrupt_vector ( IRQ_VECTOR(irq) , &new_vec);
	
	return 0;
}




void reinstall_asm_irq_handler(int irq, _go32_dpmi_seginfo *old_irq) 
{
	_go32_dpmi_set_protected_mode_interrupt_vector( IRQ_VECTOR(irq) , old_irq);
}




void reinstall_c_irq_handler(int irq, _go32_dpmi_seginfo *old_irq) 
{
	_go32_dpmi_set_protected_mode_interrupt_vector(IRQ_VECTOR(irq), old_irq);
	
}




int disable_irq(int irq) 
{
	//Writes a 1
	
	int old = 0;
	
	if (irq > 7) { //Uses PIC-1
		
		old = inportb(PIC2_MASK);
		
		outportb(PIC2_MASK, old | IRQ_MASK_BIT(irq) );
		
	} else { //Uses PIC-2
	
		old = inportb(PIC1_MASK);
		
		outportb(PIC1_MASK, old | IRQ_MASK_BIT(irq));
	}
	
	return old;
}




void enable_irq(int irq) 
{
	//Writes a 0
	
	int old = 0;
	
	if (irq > 7) { //Uses PIC-2
		
		old = inportb(PIC2_MASK);
		
		outportb(PIC2_MASK, old & (~IRQ_MASK_BIT(irq)) );
		
	} else { //Uses PIC-1
		
		old = inportb(PIC1_MASK);
		
		outportb(PIC1_MASK, old & (~IRQ_MASK_BIT(irq)) );
	}
}

